package database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import baseclasses.Course;

public class CourseDAO {

	//add course to the database
	//context is passed from baseclasses.Course object
	public static long addCourse(Context context, Course course){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COURSENAME, course.getName());
		values.put(DatabaseHelper.COURSEMONDAY, course.getWeekDayBooleans()[0]);
		values.put(DatabaseHelper.COURSETUESDAY, course.getWeekDayBooleans()[1]);
		values.put(DatabaseHelper.COURSEWEDNESDAY, course.getWeekDayBooleans()[2]);
		values.put(DatabaseHelper.COURSETHURSDAY, course.getWeekDayBooleans()[3]);
		values.put(DatabaseHelper.COURSEFRIDAY, course.getWeekDayBooleans()[4]);
		values.put(DatabaseHelper.COURSESATURDAY, course.getWeekDayBooleans()[5]);
		values.put(DatabaseHelper.COURSESUNDAY, course.getWeekDayBooleans()[6]);
		values.put(DatabaseHelper.COURSETIME, course.getStartTime().getTimeInMillis());
		values.put(DatabaseHelper.COURSECREDITS, course.getCredits());
		values.put(DatabaseHelper.COURSEENDTIME, course.getEndTime().getTimeInMillis());
		values.put(DatabaseHelper.COURSELOCATION, course.getLocation() );
		
		long result = db.insert(DatabaseHelper.COURSETABLE, null, values);

		db.close();
		return result;

	}

	//returns an array list of courses
	public static ArrayList<Course> getCourses(Context context){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		ArrayList<Course> courses = new ArrayList<Course>();

		Cursor cursor = db.query(DatabaseHelper.COURSETABLE, 
				null,null,null,null,null,null); //gets all from coursetable

		Course course;
		while(cursor.moveToNext()){
			//while there's another course row in database,
			//copy it to a new course object and add that copy to the arraylist of
			//course objects
			course = new Course();
			boolean[] weekDayBooleans = {false, false, false, false, false, false, false};

			course.setId(cursor.getInt(0));
			course.setName(cursor.getString(1));
			weekDayBooleans[0] = intToBool(cursor.getInt(2));
			weekDayBooleans[1] = intToBool(cursor.getInt(3));
			weekDayBooleans[2] = intToBool(cursor.getInt(4));
			weekDayBooleans[3] = intToBool(cursor.getInt(5));
			weekDayBooleans[4] = intToBool(cursor.getInt(6));
			weekDayBooleans[5] = intToBool(cursor.getInt(7));
			weekDayBooleans[6] = intToBool(cursor.getInt(8));
			course.setWeekDayBooleans(weekDayBooleans);
			course.setStartTime(cursor.getLong(9));
			course.setCredits(cursor.getInt(10));
			course.setEndTime(cursor.getLong(11));
			course.setLocation(cursor.getString(12));

			courses.add(course);
		}

		cursor.close();
		db.close();
		return courses;
	}

	//deletes the course from the database
	public static int deleteCourse(Context context, Course course){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		//delete Grades and Assignments associated with the course
		//before deleting the course itself
		GradeDAO.deleteGradesByCourse(context, course);
		AssignmentDAO.deleteAssignmentsByCourse(context, course);
		int result = db.delete(DatabaseHelper.COURSETABLE, "_id = "+course.getId(), null);

		
		db.close();
		return result;

	}

	//returns course object based on id provided
	public static Course getCourseById(Context context, int id){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.COURSETABLE, 
				null,"_id = "+id ,null,null,null,null);
		cursor.moveToNext();

		Course course = new Course();
		boolean[] weekDayBooleans = {false, false, false, false, false, false, false};

		course.setId(cursor.getInt(0));
		course.setName(cursor.getString(1));
		weekDayBooleans[0] = intToBool(cursor.getInt(2));
		weekDayBooleans[1] = intToBool(cursor.getInt(3));
		weekDayBooleans[2] = intToBool(cursor.getInt(4));
		weekDayBooleans[3] = intToBool(cursor.getInt(5));
		weekDayBooleans[4] = intToBool(cursor.getInt(6));
		weekDayBooleans[5] = intToBool(cursor.getInt(7));
		weekDayBooleans[6] = intToBool(cursor.getInt(8));
		course.setWeekDayBooleans(weekDayBooleans);
		course.setStartTime(cursor.getLong(9));
		course.setCredits(cursor.getInt(10));
		course.setEndTime(cursor.getLong(11));
		course.setLocation(cursor.getString(12));

		cursor.close();
		db.close();
		return course;
	}

	//returns course name on id provided
	public static String getCourseNameById(Context context, int id){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.COURSETABLE, 
				new String[]{DatabaseHelper.COURSENAME},"_id = "+id ,null,null,null,null);
		cursor.moveToNext();

		String name = cursor.getString(0);

		cursor.close();
		db.close();
		return name;
	}

	//edits course in database by updating values in course row that matches id
	public static int editCourse(Context context, Course course){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COURSENAME, course.getName());
		values.put(DatabaseHelper.COURSEMONDAY, course.getWeekDayBooleans()[0]);
		values.put(DatabaseHelper.COURSETUESDAY, course.getWeekDayBooleans()[1]);
		values.put(DatabaseHelper.COURSEWEDNESDAY, course.getWeekDayBooleans()[2]);
		values.put(DatabaseHelper.COURSETHURSDAY, course.getWeekDayBooleans()[3]);
		values.put(DatabaseHelper.COURSEFRIDAY, course.getWeekDayBooleans()[4]);
		values.put(DatabaseHelper.COURSESATURDAY, course.getWeekDayBooleans()[5]);
		values.put(DatabaseHelper.COURSESUNDAY, course.getWeekDayBooleans()[6]);
		values.put(DatabaseHelper.COURSETIME, course.getStartTime().getTimeInMillis());
		values.put(DatabaseHelper.COURSECREDITS, course.getCredits());
		values.put(DatabaseHelper.COURSEENDTIME, course.getEndTime().getTimeInMillis());
		values.put(DatabaseHelper.COURSELOCATION, course.getLocation() );
		
		int result = db.update(DatabaseHelper.COURSETABLE, values, "_id = "+course.getId(), null);

		db.close();
		return result;

	}

	//takes in an int and returns a boolean, 0 = false everything else = true
	public static boolean intToBool(int i){

		if(i != 0){
			return true;
		}
		return false;

	}

	//returns grade points 
	public static double getGradePointsByCourse(Context context, Course course){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.GRADETABLE, 
				new String[]{"SUM("+DatabaseHelper.POINTSEARNED+")"}, 
				DatabaseHelper.COURSEID+" = "+course.getId(), 
				null,null,null,null);
		//takes the sum of all points earned for all assignments under a course
		
		cursor.moveToNext();
		double result = cursor.getDouble(0); //returns grade points calculated
		cursor.close();
		db.close();

		return result;
	}
	
	//returns grade points out of
	public static double getGradePointsOutOfByCourse(Context context, Course course){

		DatabaseHelper databasehelper = new DatabaseHelper(context);
		SQLiteDatabase db = databasehelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.GRADETABLE, 
				new String[]{"SUM("+DatabaseHelper.POINTSOUTOF+")"}, 
				DatabaseHelper.COURSEID+" = "+course.getId(), 
				null,null,null,null);
		//takes the sum of all points possible for all assignments under a course
		cursor.moveToNext();
		double result = cursor.getDouble(0); //returns grade points total calculated
		cursor.close();
		db.close();

		return result;
	}
	

}
